﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using elfam.web.ViewModels;
using NHibernate.Criterion;

namespace elfam.web.Models
{
    public class Order: DomainEntity
    {
        public static string UidProperty = "Uid";
        public static string UserProperty = "User";

        public virtual User User { get; set; }
        public virtual OrderStatus Status { get; set; }
        // public virtual IList OrderStatusHistory { get; set; } 
        public virtual PaymentType PaymentType { get; set; }
        public virtual DeliverType DeliverType { get; set; }
        public virtual Contact ShippingDetails { get; set; }
        public virtual IList<OrderLine> Lines { get; set; }
        public virtual DateTime Date { get; set; }
        public virtual string Uid { get; set; }
        public virtual int Discount { get; set; }

        public static IList<OrderStatus> ProfitStatuses
        {
            get { return new List<OrderStatus>(){OrderStatus.Delivered, OrderStatus.Payed, OrderStatus.Sent};}
        }

//        public virtual IList<Outcome> Outcomes { get; set; }
        


        public Order()
        {
            ShippingDetails = new Contact();
            Lines = new List<OrderLine>();
            Date = DateTime.Now;
//            Outcomes = new List<Outcome>();
            Status = OrderStatus.WaitPayment;
        }


        public virtual OrderLine AddOrderLine(Product product, int quantity)
        {
            OrderLine orderLine = new OrderLine();
            orderLine.Price = product.Price;
            orderLine.Quantity = quantity;
            orderLine.ProductName = product.Name;
            orderLine.Product = product;
            Lines.Add(orderLine);
            return orderLine;
        }

        public virtual IEnumerable<Outcome> Outcomes()
        {
            return Lines.SelectMany(orderLine => orderLine.Outcomes);
        }

        public virtual decimal Summ()
        {
            return Outcomes().Sum(o => o.Summ());
        }

        public virtual decimal SummWithDiscount()
        {
            return Outcomes().Sum(o => o.SummWithDiscount());
        }

        public virtual decimal Profit()
        {
            return Outcomes().Sum(o => o.Profit());
        }


        public virtual void CopyFrom(OrderShippingInfo info)
        {
            DeliverType = info.DeliverType;
            PaymentType = info.PaymentType;
            ShippingDetails = info.Contact;
        }

        public virtual void ClearOutcomes()
        {
            foreach (OrderLine line in Lines)
            {
                line.Outcomes.Clear();
            }
        }

        public virtual decimal DiscountSumm()
        {
            return Outcomes().Sum(x => x.DiscountSumm());
        }
    }
}
